home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Graphics 2D / AlternateBufferDisplay / AlternateBufferDisplay.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  11.2 KB  |  402 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        AlternateBufferDisplay.c
  3.  
  4.     Contains:    Shows that copybits along with offscreen bitmaps
  5.                 can emulate alternating buffer displays at a reasonable rate.
  6.  
  7.     Written by: JW
  8.  
  9.     Copyright:    Copyright © 1991-1999 by Apple Computer, Inc., All Rights Reserved.
  10.  
  11.                 You may incorporate this Apple sample source code into your program(s) without
  12.                 restriction. This Apple sample source code has been provided "AS IS" and the
  13.                 responsibility for its operation is yours. You are not permitted to redistribute
  14.                 this Apple sample source code as "Apple sample source code" after having made
  15.                 changes. If you're going to re-distribute the source, we require that you make
  16.                 it clear in the source that the code was descended from Apple sample source
  17.                 code, but that you've made changes.
  18.  
  19.     Change History (most recent first):
  20.                 08/2000        JM                Carbonized, non-Carbon code is commented out
  21.                                             for demonstration purposes.
  22.                 7/7/1999    KG                Updated for Metrowerks Codewarror Pro 2.1
  23.                 11/12/91    JW                Created
  24.  
  25. */
  26. #include    "CarbonPrefix.h"
  27. #include    <QDOffscreen.h>
  28. #include    <Quickdraw.h>
  29. #include    <Menus.h>
  30. #include    <Dialogs.h>
  31. #include    <MacTypes.h>
  32. #include    <TextUtils.h>
  33. #include    <Resources.h>
  34. #include    <Errors.h>
  35. #include     <ToolUtils.h>
  36.  
  37. #define Gestalttest        0xA1AD
  38. #define NoTrap            0xA89F
  39.  
  40. #define    appleID            128            
  41. #define    appleMenu        0
  42. #define    aboutMeCommand    1
  43.  
  44. #define    fileID            129
  45. #define    quitCommand     1
  46.  
  47. #define    aboutMeDLOG        128
  48. #define    okButton        1
  49.  
  50. #ifndef topLeft
  51. #define topLeft(r)                      (((Point *) &(r))[0])
  52. #endif
  53.  
  54. #ifndef botRight
  55. #define botRight(r)                     (((Point *) &(r))[1])
  56. #endif
  57.  
  58. /*------------------------------------------------------*/
  59. /*    Global Variables.                                    */
  60. /*------------------------------------------------------*/
  61.  
  62. Rect                TotalRect, minRect, WinMinusScroll, InitWindowSize;
  63. WindowPtr            myWindow;
  64. Boolean                DoneFlag;
  65. MenuHandle            mymenu0, mymenu1;
  66. GWorldPtr            offscreenGWorld1, offscreenGWorld2;
  67.  
  68. /*------------------------------------------------------*/
  69. /*    showAboutMeDialog().                                */
  70. /*------------------------------------------------------*/
  71.  
  72. void showAboutMeDialog()
  73. {
  74.     GrafPtr     savePort;
  75.     DialogPtr    theDialog;
  76.     short        itemHit;
  77.  
  78.     GetPort(&savePort);
  79.     theDialog = GetNewDialog(aboutMeDLOG, nil,(WindowPtr)(-1));
  80.     //SetPort(theDialog);
  81.     SetPortDialogPort(theDialog);
  82.  
  83.     do {
  84.         ModalDialog(nil, &itemHit);
  85.     } while (itemHit != okButton);
  86.  
  87.     //CloseDialog(theDialog);
  88.     DisposeDialog(theDialog);
  89.     
  90.     SetPort(savePort);
  91.     return;
  92. }
  93.  
  94. /*------------------------------------------------------*/
  95. /*    init().                                                */
  96. /*------------------------------------------------------*/
  97.  
  98. void init()
  99. {
  100.     RgnHandle            tempRgn;
  101.     Rect                BaseRect;
  102.     GDHandle            SaveGD;
  103.     CGrafPtr            SavePort;
  104.     QDErr                 myerr;
  105.     RGBColor            black = {0,0,0}, white = {65535,65535,65535};
  106.  
  107.     //InitGraf(&qd.thePort);
  108.     //InitWindows();
  109.     //InitDialogs(nil);
  110.     InitCursor();
  111.     FlushEvents(everyEvent, 0);
  112.     
  113.     /*    Set up menus.    */
  114.     mymenu0 = GetMenu(appleID);
  115.     //AppendResMenu(mymenu0, 'DRVR');
  116.     InsertMenu(mymenu0,0);
  117.     mymenu1 = GetMenu(fileID);
  118.     InsertMenu(mymenu1,0);
  119.     DrawMenuBar();
  120.  
  121.     /* Main initialization.    */
  122.     DoneFlag = false;
  123.  
  124.     /*    Set Rects.    */
  125.     SetRect(&BaseRect, 40, 60, 490, 540);
  126.     SetRect(&WinMinusScroll, BaseRect.left-40, BaseRect.top-60, BaseRect.right-56, 
  127.                 BaseRect.bottom - 76);
  128.     SetRect(&InitWindowSize, WinMinusScroll.left, WinMinusScroll.top, 
  129.                             WinMinusScroll.right, WinMinusScroll.bottom);
  130.     tempRgn =(RgnHandle)GetGrayRgn();
  131.     HLock ((Handle) tempRgn);
  132.     //TotalRect = (**tempRgn).rgnBBox;
  133.     GetRegionBounds(tempRgn, &TotalRect);
  134.     //SetRect(&minRect, 80, 80, (**tempRgn).rgnBBox.right - 40, 
  135.     //            (**tempRgn).rgnBBox.bottom - 40);
  136.     SetRect(&minRect, 80, 80, TotalRect.right - 40, TotalRect.bottom - 40);
  137.     HUnlock ((Handle) tempRgn);
  138.  
  139.     /*    Open window and set up picture.    */
  140.     GetGWorld (&SavePort, &SaveGD);
  141.     myWindow = NewCWindow(nil, &BaseRect, "\p", true, zoomDocProc, 
  142.                             (WindowPtr) -1, true, 150);
  143.     SetGWorld((CGrafPtr)myWindow, SaveGD);
  144.     DrawGrowIcon (myWindow);
  145.  
  146.     GetGWorld (&SavePort, &SaveGD);
  147.     
  148.     /*    Make first offscreen buffer.    */
  149.     if (myerr=NewGWorld (&offscreenGWorld1, 0, &InitWindowSize, nil, nil, 0))
  150.         Debugger();
  151.     SetGWorld (offscreenGWorld1, nil);
  152.     RGBForeColor(&white);
  153.     PaintRect (&InitWindowSize);
  154.     
  155.     /*    Make second offscreen buffer.    */
  156.     if (NewGWorld (&offscreenGWorld2, 0, &InitWindowSize, nil, nil, 0))
  157.         Debugger();
  158.     SetGWorld (offscreenGWorld2, nil);
  159.     RGBForeColor(&black);
  160.     PaintRect (&InitWindowSize);
  161.     
  162.     SetGWorld (SavePort, SaveGD);
  163. }
  164.  
  165. /*------------------------------------------------------*/
  166. /*    doCommand().                                        */
  167. /*------------------------------------------------------*/
  168.  
  169. void doCommand(mResult)
  170.     long    mResult;
  171. {
  172.     int                     theMenu, theItem;
  173.     //char                    daName[256];
  174.     //GrafPtr                 savePort;
  175.  
  176.     theItem = LoWord(mResult);
  177.     theMenu = HiWord(mResult);
  178.     
  179.     switch (theMenu) {
  180.         case appleID:
  181.             if (theItem == aboutMeCommand)
  182.                 showAboutMeDialog();
  183.             else {
  184.                 /*SetMenuItemText(mymenu0, theItem, c2pstr(daName));
  185.                 GetPort(&savePort);
  186.                 (void) OpenDeskAcc(daName);
  187.                 SetPort(savePort);*/
  188.             }
  189.             break;
  190.  
  191.         case fileID:
  192.             switch (theItem) {
  193.                 case quitCommand:
  194.                     DoneFlag = true;
  195.                     break;
  196.                 default:
  197.                     break;
  198.                 }
  199.             break;
  200.     }
  201.     HiliteMenu(0);
  202.     return;
  203. }
  204.  
  205. /*------------------------------------------------------*/
  206. /*    draw().                                                */
  207. /*------------------------------------------------------*/
  208.  
  209. void draw()
  210. {
  211.     RGBColor        black = {0,0,0}, White = {65535,65535,65535};
  212.     long            delay;
  213.     Str255            theString;
  214.     GDHandle        screensDevice;
  215.     Rect            area;
  216.     int                i;
  217.     GrafPtr            oldPort;
  218.     RgnHandle        rgnHandle = NewRgn();
  219.     
  220.     GetPort(&oldPort);
  221.     SetPortWindowPort(myWindow);
  222.         
  223.     RGBForeColor (&black);
  224.     RGBBackColor (&White);
  225.     EraseRect(&WinMinusScroll);
  226.     
  227.  
  228.     area = WinMinusScroll;
  229.     
  230.     LocalToGlobal(&topLeft(area));
  231.     LocalToGlobal(&botRight(area));
  232.     
  233.     screensDevice = GetMaxDevice(&area);
  234.     if (screensDevice != nil) {
  235.         //(**(**offscreenGWorld1->portPixMap).pmTable).ctSeed = (**(**(**screensDevice).gdPMap).pmTable).ctSeed;
  236.         //(**(**offscreenGWorld2->portPixMap).pmTable).ctSeed = (**(**(**screensDevice).gdPMap).pmTable).ctSeed;
  237.         (**(**(GetPortPixMap(offscreenGWorld1))).pmTable).ctSeed = (**(**(**screensDevice).gdPMap).pmTable).ctSeed;
  238.         (**(**(GetPortPixMap(offscreenGWorld2))).pmTable).ctSeed = (**(**(**screensDevice).gdPMap).pmTable).ctSeed;
  239.     }
  240.     delay = TickCount();
  241.     for (i=0; i< 10; i++) {
  242.         /*CopyBits ((BitMap *) *offscreenGWorld2->portPixMap, &myWindow->portBits, 
  243.                     &InitWindowSize, &WinMinusScroll, srcCopy, nil);
  244.         CopyBits ((BitMap *) *offscreenGWorld1->portPixMap, &myWindow->portBits, 
  245.                     &InitWindowSize, &WinMinusScroll, srcCopy, nil);*/
  246.         CopyBits ((BitMap *) *(GetPortPixMap(offscreenGWorld2)), GetPortBitMapForCopyBits(GetWindowPort(myWindow)), 
  247.                     &InitWindowSize, &WinMinusScroll, srcCopy, nil);
  248.         QDFlushPortBuffer(GetWindowPort(myWindow), GetPortVisibleRegion(GetWindowPort(myWindow), rgnHandle));
  249.         CopyBits ((BitMap *) *(GetPortPixMap(offscreenGWorld1)), GetPortBitMapForCopyBits(GetWindowPort(myWindow)), 
  250.                     &InitWindowSize, &WinMinusScroll, srcCopy, nil);
  251.         QDFlushPortBuffer(GetWindowPort(myWindow), GetPortVisibleRegion(GetWindowPort(myWindow), rgnHandle));
  252.     }
  253.     delay = TickCount() - delay;
  254.     MoveTo(50,50);
  255.     NumToString(delay, theString);
  256.     DrawString(theString);
  257.     QDFlushPortBuffer(GetWindowPort(myWindow), GetPortVisibleRegion(GetWindowPort(myWindow), rgnHandle));
  258.     
  259.     SetPort(oldPort);
  260.     DisposeRgn(rgnHandle);
  261. }
  262.  
  263. /*------------------------------------------------------*/
  264. /*    main().                                                */
  265. /*------------------------------------------------------*/
  266.  
  267. main()
  268. {
  269.     char            key;
  270.     Boolean            track;
  271.     long            growResult;
  272.     EventRecord     myEvent;
  273.     WindowPtr        whichWindow;
  274.     int                yieldTime;
  275.     Rect            tempRect1;
  276.  
  277.     init();
  278.     yieldTime = 0;
  279.     for ( ;; ) {
  280.         if (DoneFlag)
  281.             ExitToShell();
  282.          
  283.         if (WaitNextEvent(everyEvent, &myEvent, yieldTime, nil)) {
  284.             switch (myEvent.what) {
  285.                 case mouseDown:
  286.                     switch (FindWindow(myEvent.where, &whichWindow)) {
  287.                         case inSysWindow:
  288.                             //SystemClick(&myEvent, whichWindow);
  289.                             break;
  290.                         case inMenuBar:
  291.                             doCommand(MenuSelect(myEvent.where));
  292.                             break;
  293.                         case inContent:
  294.                             break;
  295.                         case inDrag:
  296.                             DragWindow (whichWindow, myEvent.where, &TotalRect);
  297.                             draw();
  298.                             DrawGrowIcon (whichWindow);
  299.                             break;
  300.                         case inGrow:
  301.                             growResult = GrowWindow (whichWindow, myEvent.where,
  302.                                                     &minRect);
  303.                             SizeWindow(whichWindow, LoWord(growResult), 
  304.                                     HiWord(growResult), true);
  305.                             //EraseRect(&whichWindow->portRect);
  306.                             EraseRect(GetPortBounds(GetWindowPort(whichWindow), &tempRect1));
  307.                             /*SetRect(&WinMinusScroll, whichWindow->portRect.left, 
  308.                                     whichWindow->portRect.top, 
  309.                                     whichWindow->portRect.right-20, 
  310.                                     whichWindow->portRect.bottom - 20);*/
  311.                             SetRect(&WinMinusScroll, tempRect1.left, 
  312.                                     tempRect1.top, 
  313.                                     tempRect1.right-20, 
  314.                                     tempRect1.bottom - 20);
  315.                             draw();
  316.                             DrawGrowIcon (whichWindow);
  317.                             break;
  318.                         case inGoAway:
  319.                             track = TrackGoAway (whichWindow, myEvent.where);
  320.                             if (track) {
  321.                                 //CloseWindow (whichWindow);
  322.                                 DoneFlag = true;
  323.                                 }
  324.                             break;
  325.                         case inZoomIn:
  326.                             track = TrackBox (whichWindow, myEvent.where, inZoomIn);
  327.                             if (track) {
  328.                                 ZoomWindow (whichWindow, inZoomIn, true);
  329.                                 //EraseRect(&whichWindow->portRect);
  330.                                 EraseRect(GetPortBounds(GetWindowPort(whichWindow), &tempRect1));
  331.                                 /*SetRect(&WinMinusScroll, whichWindow->portRect.left, 
  332.                                         whichWindow->portRect.top, 
  333.                                         whichWindow->portRect.right-20, 
  334.                                         whichWindow->portRect.bottom - 20);*/
  335.                                 SetRect(&WinMinusScroll, tempRect1.left, 
  336.                                     tempRect1.top, 
  337.                                     tempRect1.right-20, 
  338.                                     tempRect1.bottom - 20);
  339.                                 draw();
  340.                                 DrawGrowIcon (whichWindow);
  341.                                 }
  342.                             break;
  343.                         case inZoomOut:
  344.                             track = TrackBox (whichWindow, myEvent.where, inZoomOut);
  345.                             if (track) {
  346.                                 ZoomWindow (whichWindow, inZoomOut, true);
  347.                                 //EraseRect(&whichWindow->portRect);
  348.                                 EraseRect(GetPortBounds(GetWindowPort(whichWindow), &tempRect1));
  349.                                 //SetRect(&WinMinusScroll, whichWindow->portRect.left, 
  350.                                 //        whichWindow->portRect.top, 
  351.                                 //        whichWindow->portRect.right-20, 
  352.                                 //        whichWindow->portRect.bottom - 20);
  353.                                 SetRect(&WinMinusScroll, tempRect1.left, 
  354.                                     tempRect1.top, 
  355.                                     tempRect1.right-20, 
  356.                                     tempRect1.bottom - 20);
  357.                                 draw();
  358.                                 DrawGrowIcon (whichWindow);
  359.                                 }
  360.                             break;
  361.                         default:
  362.                             break;
  363.                         }
  364.                     break;
  365.                 case keyDown:
  366.                 case autoKey:
  367.                     key = myEvent.message & charCodeMask;
  368.                     if ( myEvent.modifiers & cmdKey )
  369.                         if ( myEvent.what == keyDown )
  370.                             doCommand(MenuKey(key));
  371.                     break;
  372.                 case updateEvt:
  373.                     if ((WindowPtr) myEvent.message == myWindow) {
  374.                         BeginUpdate((WindowPtr) myWindow);
  375.                         draw();
  376.                         DrawGrowIcon (whichWindow);
  377.                         EndUpdate((WindowPtr) myWindow);    
  378.                     }
  379.                     break;
  380.                 case diskEvt:
  381.                     break;
  382.                 case activateEvt:
  383.                     if (myEvent.modifiers & activeFlag) {
  384.                         draw();
  385.                         DrawGrowIcon(myWindow);
  386.                     }
  387.                 case app4Evt:
  388.                     if ((myEvent.message << 31) == 0) {
  389.                         yieldTime = 30;
  390.                         }
  391.                     else {
  392.                         yieldTime = 0;
  393.                         //SetPort((WindowPtr) myWindow);
  394.                         SetPortWindowPort(myWindow);
  395.                         }
  396.                     break; 
  397.                 default:
  398.                     break;
  399.                 }
  400.             }
  401.         }
  402. }